<?php

namespace app\admin\controller\broadcast;

use app\common\controller\Backstage;

use app\common\model\work_area\HkDeviceSerial;
use app\common\service\broadcast\IpBroadcastService;
use app\common\service\RedisQuantity;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;

/**
 * @Apidoc\Title("IP广播相关")
 */
class IpBroadcast extends Backstage
{

    //中间件不验证的方法
    protected $middleware = [
        'app\\middleware\\Admin' => [
            'except' => ['info']
        ]
    ];

    /**
     * @Apidoc\Title("IP广播列表")
     * @Apidoc\Url("/admin/broadcast.IpBroadcast/listApi")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("keyword", type="string", desc="关键字")
     * @Apidoc\Param("keyword_time", type="array", desc="数组0开始 1结束")
     * @Apidoc\Param("page", type="int", desc="第几页默认第1页")
     * @Apidoc\Param("page_size", type="int", desc="每页几条默认10条")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     * @Apidoc\Returned("total", type="int", desc="总条数")
     */
    public function listApi()
    {
        try {
            $param = $this->request->param();
            $page_size = empty($param['page_size']) ? 10 : $param['page_size'];
            //条件
            $where = [];
            //模糊查询
            if (!empty($param['keyword'])) {
                $where[] = ['a.name|a.dev_name|a.code', 'like', "%{$param['keyword']}%"];
            }
            //时间查询
            if (!empty($param['keyword_time'])) {
                $time = 'a.create_time';//查询时间字段
                $timeWhere = timeWhere($param['keyword_time'][0], $param['keyword_time'][1]);
                $where[] = [$time, '>=', $timeWhere[0]];
                $where[] = [$time, '<=', $timeWhere[1] + 86399];
            }
            //排序
            $order = ['a.id' => 'DESC'];
            if (!empty($param['order']) && !empty($param['order_name'])) {
                $order_name = $param['order_name'];
                switch ($param['order']) {
                    case 1:
                        $order = [$order_name => 'DESC'];
                        break;
                    case 2:
                        $order = [$order_name => 'ASC'];
                        break;
                }
            }
            $list_ = \app\common\model\broadcast\IpBroadcast::alias("a")
                ->where($where)
                ->order($order)
                ->field('a.id,a.code,a.name,a.hk_device_serial_id,a.dev_name,a.remarks,a.create_time')
                ->paginate($page_size)
                ->toArray();
            $list = $list_ ['data'];
            foreach ($list as $key => $val) {
                $nameb = HkDeviceSerial::where('id', 'in', $val['hk_device_serial_id'])->column('name');
                $list[$key]['nameb'] = implode(',', $nameb);
                unset($nameb);
            }
            $device_rod = \app\common\model\broadcast\DeviceRod::field('id,name,work_area_stand_id')->select();
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '成功', [
            'list' => $list,
            'total' => $list_['total'],
            'page_size' => $page_size,
            'device_rod' => $device_rod,
        ]);
    }


    /**
     * @Apidoc\Title("广播同步")
     * @Apidoc\Url("/admin/broadcast.IpBroadcast/synchronous")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function synchronous()
    {
        try {
            $result = JWTAuth::auth();
            $admin_id = $result['admin_id'];
            IpBroadcastService::synchronous();
            if (!RedisQuantity::FrequencyLimit(md5(request()->controller() . request()->action() . $admin_id), 2)) {
                throw new \Exception('请求频繁,稍后再试');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '操作成功');
    }

    /**
     * @Apidoc\Title("广播设备详情")
     * @Apidoc\Url("/admin/broadcast.IpBroadcast/info")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", desc="列表id")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function info()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|id' => 'require',
            ])->check($param);
            $info = \app\common\model\broadcast\IpBroadcast::where(['id' => $param['id']])->find();
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        return ajaxJson(1, '成功', $info);
    }

    /**
     * @Apidoc\Title("广播设备修改")
     * @Apidoc\Url("/admin/broadcast.IpBroadcast/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int", desc="列表id")
     * @Apidoc\Param("device_rod_id", type="int", desc="装置id")
     * @Apidoc\Param("work_area_stand_id", type="int", desc="工点ID")
     * @Apidoc\Param("type", type="int", desc="1云播放 2长链接推送本地播放")
     * @Apidoc\Param("hk_device_serial_id", type="int", desc="摄像头id")
     * @Apidoc\Param("remarks", type="string", desc="备注")
     * @Apidoc\Param("ip", type="string", desc="连接ip")
     * @Apidoc\Param("authorization", type="string", desc="广播秘钥")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function edit()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|id' => 'require',
                'hk_device_serial_id|摄像头id' => 'require',
                'remarks|备注' => 'require',
                'device_rod_id|装置id' => 'require',
            ])->check($param);
            unset($param['create_time']);
            \app\common\model\broadcast\IpBroadcast::update($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        return ajaxJson(1, '成功');
    }


    /**
     * @Apidoc\Title("广播设备新增")
     * @Apidoc\Url("/admin/broadcast.IpBroadcast/add")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("device_rod_id", type="int", desc="装置id")
     * @Apidoc\Param("work_area_stand_id", type="int", desc="工点ID")
     * @Apidoc\Param("type", type="int", desc="1云播放 2长链接推送本地播放")
     * @Apidoc\Param("code", type="int", desc="设备code")
     * @Apidoc\Param("name", type="string", desc="设备名称")
     * @Apidoc\Param("dev_name", type="string", desc="设备别名")
     * @Apidoc\Param("hk_device_serial_id", type="string", desc="摄像头id多个逗号隔开")
     * @Apidoc\Param("remarks", type="string", desc="备注")
     * @Apidoc\Param("ip", type="string", desc="连接ip")
     * @Apidoc\Param("authorization", type="string", desc="广播秘钥")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function add()
    {
        try {
            $param = $this->request->param();
            Validate([
                'device_rod_id|装置id' => 'require',
                'type|播放类型' => 'require',
                'code|code' => 'require',
                'name|广播名称' => 'require',
                'hk_device_serial_id|摄像头id' => 'require',
                'authorization|广播秘钥' => 'require',
            ])->check($param);
            \app\common\model\broadcast\IpBroadcast::create($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        return ajaxJson(1, '成功');
    }


    /**
     * @Apidoc\Title("摄像头列表")
     * @Apidoc\Url("/admin/broadcast.IpBroadcast/cameraList")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function cameraList()
    {
        try {
            $list = HkDeviceSerial::field(['id', 'name'])->select()->toArray();
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return ajaxJson(0, $e->getMessage());
        }
        return ajaxJson(1, '成功', $list);
    }

}